2 Problem: 10249 - The grand dinner
3 Andrés Mejía-Posada (andmej@gmail.com)
28 #define foreach(x, v) for (typeof (v).begin() x = (v).begin(); x != (v).end(); ++x)
29 #define D(x) cout << #x " is " << x << endl
30 #define For(i, a, b) for (int i=(a); i<(b); ++i)
40 bool findPath(int n
, int src
, int sink
){
41 fill(prev
, prev
+n
, -1);
45 int u
= q
.front(); q
.pop();
46 if (u
== sink
) return true;
49 if (v
!= src
&& prev
[v
] == -1 && flow
[u
][v
] < cap
[u
][v
]){
58 int maxFlow(int n
, int src
, int sink
){
59 memset(flow
, 0, sizeof flow
);
61 while (findPath(n
, src
, sink
)){
63 for (int u
= sink
; prev
[u
] != -1; u
= prev
[u
]){
64 neck
= min(neck
, cap
[prev
[u
]][u
] - flow
[prev
[u
]][u
]);
66 for (int u
= sink
; prev
[u
] != -1; u
= prev
[u
]){
67 flow
[prev
[u
]][u
] += neck
;
68 flow
[u
][prev
[u
]] -= neck
;
75 inline void add_edge(int u
, int v
, int c
){
76 g
[u
].push_back(v
); g
[v
].push_back(u
); cap
[u
][v
] = c
;
81 printf("%d %d\n", m
, n
);
82 for (int i
=0; i
<m
; ++i
){
85 for (int i
=0; i
<n
; ++i
){
92 //bigcase(); return 0;
94 while (scanf("%d %d", &nteams
, &ntables
)==2 && nteams
&& ntables
){
95 int people
= 0, spots
= 0;
96 const int source
= nteams
+ ntables
, sink
= source
+ 1;
97 for (int i
=0; i
<=sink
; ++i
) g
[i
].clear();
98 memset(cap
, 0, sizeof cap
);
99 for (int i
=0, c
; i
<nteams
; ++i
){
101 add_edge(source
, i
, c
);
104 for (int i
=0, c
; i
<ntables
; ++i
){
106 add_edge(nteams
+i
, sink
, c
);
109 if (people
> spots
){ printf("0\n"); continue; }
110 for (int i
=0; i
<nteams
; ++i
){
111 for (int j
=0; j
<ntables
; ++j
){
112 add_edge(i
, nteams
+j
, 1);
115 int f
= maxFlow(nteams
+ ntables
+ 2, source
, sink
);
120 for (int i
=0; i
<nteams
; ++i
){
125 if (!first
) printf(" ");
127 printf("%d", j
-nteams
+1);